package V002_CollisionDetection;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

/**
 *
 * @author Smai
 *
 */
public class Main {

  public static BufferedImage bImg;
  private int width = 1024;
  private int height = 640;
  private Canvas canvas;
  private Map map;
  private Game game;
  private ActionListener actionListener;
  private Player player;

  public Main() {
    actionListener = new ActionListener();
    canvas = new Canvas();
    //Mista että kun muutat äijän aloitus kohtaa muuta myös törmäystarkistus
    //Peli tarkastaa vain pelaajan ympärillä vieressä olevat palikat.
    player = new Player(10, 10, 32, 32, "pelaaja", "Player", true);
    map = new Map(game, player);
    game = new Game(actionListener, player,map);

    JFrame frame = new JFrame();

    canvas.addKeyListener(actionListener);
    canvas.setPreferredSize(new Dimension(width, height));

    frame.add(canvas);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setFocusable(false);
    frame.setLocation(400, 300);
    canvas.requestFocus();
  }

  public static void main(String[] args) {
    Main main = new Main();
    main.loop();
  }

  //Pelin silmukka
  public void loop() {

    bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics gbi = bImg.createGraphics();

    while (true) {
      try {

        game.update();
        map.piirraKartta(gbi, width, height);
        canvas.repaint();

        Thread.sleep(33);
      } catch (Exception ex) {
        ex.printStackTrace();
        break;
      }
    }
  }
}
